/**
 * 角色管理
 * 
 * @return {}
 */
var priRolesSearch = function() {
	var rolesDeptUrl = __ctxPath + "/systemDept/selectDeptTreeById?deptRoot=1";
	var roleDept = new TreeSelector("treeRoleDept", rolesDeptUrl, "所属部门",
			"priRoles_deptId", true);
	var priRolesField = new Ext.form.FieldSet({
				title : '查询条件',
				id : 'priRolesField',
				collapsible : true,
				buttonAlign : 'center',
				buttons : [{
							text : '查询',
							handler : function() {
								queryPriRoles();
							}
						}, {
							text : '重置',
							handler : function() {
								Ext.getCmp('priRolesForm').getForm().reset();
							}
						}],
				items : [{
					layout : 'column',
					border : false,
					items : [{
								columnWidth : .25,
								layout : 'form',
								border : false,
								items : [{
											xtype : 'textfield',
											name : 'Q_name_S_LK',
											anchor : '100%',
											fieldLabel : '角色名称'
										}]
							}, {
								columnWidth : .25,
								layout : 'form',
								border : false,
								items : [{
											xtype : 'textfield',
											name : 'Q_description_S_LK',
											anchor : '100%',
											fieldLabel : '角色描述'
										}]
							}, {
								columnWidth : .25,
								layout : 'form',
								border : false,
								items : [roleDept]
							}, {
								columnWidth : .25,
								layout : 'form',
								border : false,
								items : [{
									xtype : "combo",
									fieldLabel : "状态",
									hiddenName : "Q_isUseable_S_EQ",
									emptyText : "选择状态",
									store : new Ext.data.ArrayStore({
												fields : ["value", "text"],
												data : [['1', "可用"],
														['0', "禁用"], ['', '全部']]
											}),
									valueField : "value",
									displayField : "text",
									mode : "local",
									value : '1',
									triggerAction : "all"
								}]
							}, {
								columnWidth : .25,
								layout : 'form',
								border : false,
								items : [{
											name : "Q_deptId_L_EQ",
											id : "priRoles_deptId",
											xtype : "hidden"
										}]
							}]
				}]
			});
	fieldSetListeners(['priRolesField', 'priRolesForm', 'priRolesGrid'], [0.20,0.80]);
	var priRolesForm = new Ext.form.FormPanel({
				id : 'priRolesForm',
				anchor : '100% 20%',
				frame : true,
				labelAlign : 'right',
				bodyBorder : false,
				items : [priRolesField]
			})
	return priRolesForm;
}

var priRolesResult = function() {
	var priRolesSm = new Ext.grid.CheckboxSelectionModel();// 创建选择模式对象
	var priRolesCm = new Ext.grid.ColumnModel([// 定义表的每列（对应表头信息）
	priRolesSm, new Ext.grid.RowNumberer(), {
				header : "角色编号",
				sortable : true,
				width : 70,
				dataIndex : "id"
			}, {
				header : "角色名称",
				sortable : true,
				width : 140,
				dataIndex : "name"
			}, {
				header : "角色描述",
				width : 190,
				dataIndex : "description"
			}, {
				header : "所属部门",
				sortable : true,
				width : 125,
				dataIndex : "deptName"
			}, {
				header : "创建时间",
				sortable : true,
				width : 135,
				dataIndex : "createdAt"
			}, {
				header : "创建人",
				width : 60,
				dataIndex : "createdMan"
			}, {
				header : "修改时间",
				width : 135,
				sortable : true,
				dataIndex : "updatedAt"
			}, {
				header : "修改人",
				width : 60,
				dataIndex : "updatedMan"
			}, {
				header : "状态",
				width : 50,
				dataIndex : "isUseable",
				renderer : function(a) {
					var b = "";
					if (a == "1") {
						b += '可用';
					} else {
						b += '禁用';
					}
					return b;
				}
			}, {
				header : "管理",
				width : 180,
				dataIndex : "userId",
				sortable : false,
				renderer : function(h, g, c, e, i) {
					var a = c.data.id;
					var b = c.data.name;
					var d = "";
					if (a != -1) {
						if (isGranted("rolesManage_删除角色")) {
							d = '<a href="#" onclick="remove(' + a
									+ ')">删除</a>';
						}
						if (isGranted("rolesManage_修改角色")) {
							d += '&nbsp;&nbsp;<a  href="#" onclick="updatePriRole('
									+ a + ')">修改</a>';
						}
						if (isGranted("rolesManage_角色授权功能")) {
							d += '&nbsp;&nbsp;<a  href="#" onclick="grant(' + a
									+ ",'" + b + "')\">授权功能</a>";
						}
						if (isGranted("rolesManage_查看角色人员")) {
							d += '&nbsp;&nbsp;<a  href="#" onclick="lookRole('
									+ a + ",'" + b + "')\">查看人员</a>";
						}
					}
					return d;
				}
			}]);

	// 定义分析器
	var priRolesReader = new Ext.data.JsonReader({
				totalProperty : "totalCount",
				root : "list",
				fields : [{
							name : "id"
						}, {
							name : "name"
						}, {
							name : "description"
						}, {
							name : "deptId"
						}, {
							name : "createdAt"
						}, {
							name : "createdMan"
						}, {
							name : "updatedAt"
						}, {
							name : "updatedMan"
						}, {
							name : "isUseable"
						}, {
							name : 'deptName'
						}]
			});

	var priRolesDs = new Ext.data.Store({// 取数据
		autoLoad : true,
		proxy : new Ext.data.HttpProxy({
					url : __ctxPath + "/systemRole/selectAllRoleMessage"
				}),
		remoteSort : false,
		baseParams : {
			start : 0,
			limit : 25
		},
		listeners : {
			beforeload : {
				fn : function(thiz, options) {
					Ext.apply(thiz.baseParams, Ext.getCmp('priRolesForm')
									.getForm().getValues());
				}
			}
		},
		reader : priRolesReader
	});

	var priRolesTb = new Ext.Toolbar({// 定义grid的toolbar
		id : "priRolesTb",
		height : 25,
		items : []
	});
	if (isGranted("rolesManage_新建角色")) {
		priRolesTb.add(new Ext.Button({
					text : "新建角色",
					width : 80,
					pressed : true,
					handler : function() {
						addPriRole();
					}
				}));
	}
	if (isGranted("rolesManage_删除角色")) {
		priRolesTb.add('-', new Ext.Button({
							width : 80,
							pressed : true,
							text : "删除角色",
							handler : function() {
								var d = Ext.getCmp("priRolesGrid");
								var b = d.getSelectionModel().getSelections();
								if (b.length == 0) {
									Ext.MessageBox.show({
												title : "操作信息",
												msg : "请选择要删除的记录!",
												buttons : Ext.MessageBox.OK,
												icon : Ext.MessageBox.WARNING
											});
									return;
								}
								var e = Array();
								var f = "";
								for (var c = 0; c < b.length; c++) {
									if (b[c].data.id != -1) {
										e.push(b[c].data.id);
									} else {
										f += b[c].data.name + ",";
									}
								}
								if (f == "") {
									remove(e);
								} else {
									Ext.MessageBox.show({
												title : "操作信息",
												msg : f + "不能被删除!",
												buttons : Ext.MessageBox.OK,
												icon : Ext.MessageBox.WARNING
											});
								}
							}
						}));
	}
	var priRolesGrid = new Ext.grid.GridPanel({
				id : 'priRolesGrid',
				store : priRolesDs,
				cm : priRolesCm,
				sm : priRolesSm,
				autoScroll : true,
				stripeRows : true,
				columnLines : true,
				loadMask : true,
				loadMask : {
					msg : "正在加载数据，请稍侯……"
				},
				viewConfig : {},
				anchor : '100% 80%',
				frame : true,
				tbar : priRolesTb,
				bbar : new PG.PagingBar({
							store : priRolesDs
						})
			});
	return priRolesGrid;
};
// 查询
function queryPriRoles() {
	var grid = Ext.getCmp("priRolesGrid");
	var form = Ext.getCmp("priRolesForm");
	if (form.getForm().isValid()) {
		grid.getStore().load();
	}
};
// 新建
function addPriRole() {
	Ext.Loader.load([__ctxPath + '/js/system/roleForm.js'],
			function() {
				addPriRoleWin().show();
			});
};
// 修改
function updatePriRole(roleId) {
	Ext.Loader.load([__ctxPath + '/js/system/roleForm.js'],
			function() {
				addPriRoleWin(roleId).show();
			});
};
// 删除
function remove(b) {
	var a = Ext.getCmp("AppRoleGrid");
	Ext.Msg.confirm("信息确认", "您确认要删除该记录吗？", function(c) {
				if (c == "yes") {
					Ext.Ajax.request({
								url : __ctxPath + "/systemRole/deleteRoleById",
								params : {
									ids : b
								},
								method : "post",
								success : function() {
									Ext.MessageBox.show({
												title : "操作信息",
												msg : "成功删除所选记录!",
												buttons : Ext.MessageBox.OK,
												icon : Ext.MessageBox.INFO
											});
									queryPriRoles();
								}
							});
				}
			});
};
// 授权功能
function grant(id, name) {
	var rights = [];
	Ext.Ajax.request({
				url : __ctxPath + "/systemFunction/selectOwnFunctionByRoleId",
				method : "POST",
				params : {
					roleId : id
				},
				success : function(f, h) {
					var funDate = Ext.util.JSON.decode(f.responseText).data;
					if (funDate != null) {
						rights = funDate.split(',');
					};
					Ext.Loader.load([__ctxPath
									+ '/js/system/roleGrantRightView.js'],
							function() {
								roleGrantRightView(id, name, rights).show();
							});
				},
				failure : function(f, g) {
					Ext.MessageBox.show({
								title : "操作信息",
								msg : "加载权限出错!",
								buttons : Ext.MessageBox.OK,
								icon : Ext.MessageBox.ERROR
							});
				},
				scope : this
			});
};
// 查看角色对应的人员
function lookRole(roleId, name) {
	var centerPanel = Ext.getCmp("centerTabPanel");
	var tab = centerPanel.getItem('_lookRole');
	if (tab == null) {
		var tab = centerPanel.add({
					id : '_lookRole',
					title : '角色【' + name + '】对应的人员',
					layout : 'fit',
					closable : true,
					autoScroll : true
				});
	} else {
		tab.removeAll();
	}
	centerPanel.activate(tab);
	Ext.Loader.load(
			[__ctxPath + '/js/system/queryCheckPersonnel.js'],
			function() {
				Ext.getCmp("centerTabPanel").getActiveTab()
						.add(checkPersonnelResult(roleId));
				Ext.getCmp("centerTabPanel").getActiveTab().doLayout();
				fieldSetListeners(['queryCheckPersonnelGrid'], [0.85, 0.15]);
				queryLookRole();
			});
};

Ext.getCmp("centerTabPanel").getActiveTab().add(priRolesSearch(),
		priRolesResult());
Ext.getCmp("centerTabPanel").getActiveTab().doLayout();